package com.qa.service.impl;

import com.qa.constant.MsgConstant;
import com.qa.constant.StatusConstant;
import com.qa.dto.NoteAddDTO;
import com.qa.dto.NoteSelfQueryDTO;
import com.qa.dto.NoteUpdateDTO;
import com.qa.entity.Note;
import com.qa.mapper.NoteMapper;
import com.qa.result.Result;
import com.qa.service.NoteService;
import com.qa.utils.CommonUtil;
import com.qa.utils.RedisIdWorker;
import com.qa.utils.UserUtil;
import com.qa.vo.NoteSelfVO;
import com.qa.vo.NoteVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.qa.constant.CacheConstant.NOTE_KEY_PREFIX;
import static com.qa.constant.MsgConstant.ADD_NOTE_FAIL;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteMapper noteMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 根据题目查询(公开、存在的)笔记,带有创建者昵称
     *
     * @param questionId 题目id
     * @return Result
     */
    @Override
    public Result getNotesByQuestionId(Long questionId) {
        // 当前用户id
        Long userId = UserUtil.getCurrentUserId();
        List<NoteVO> notes = noteMapper.getNotesByQuestionId(questionId, userId);
        return Result.success(notes);
    }

    /**
     * 添加笔记
     *
     * @param noteAddDTO 笔记
     * @return Result
     */
    @Override
    public Result addNote(NoteAddDTO noteAddDTO) {
        Note note = new Note();
        BeanUtils.copyProperties(noteAddDTO, note);
        // 生成id
        RedisIdWorker redisIdWorker = new RedisIdWorker(stringRedisTemplate);
        long noteId = redisIdWorker.nextId(NOTE_KEY_PREFIX);
        // 笔记id
        note.setId(noteId);

        boolean success = noteMapper.addNote(note);
        if (!success) {
            return Result.error(ADD_NOTE_FAIL);
        }
        return Result.success();
    }

    /**
     * 条件查询自己的笔记（存在的）
     *
     * @param noteSelfQueryDTO 查询条件
     * @return Result
     */
    @Override
    public Result getNotesByCondition(NoteSelfQueryDTO noteSelfQueryDTO) {
        Note note = new Note();
        BeanUtils.copyProperties(noteSelfQueryDTO, note);
        // 设置当前用户id
        note.setCreator(UserUtil.getCurrentUserId());
        // 设置为已存在
        note.setStatus(StatusConstant.EXISTENCE);
        // 查询结果
        List<Note> notes = noteMapper.getNotesByCondition(note);
        // 返回前端的结果
        List<NoteSelfVO> noteSelfVos = CommonUtil.copyList(notes, NoteSelfVO.class);
        return Result.success(noteSelfVos);
    }

    /**
     * 更新笔记
     *
     * @param noteUpdateDTO 笔记
     * @return Result
     */
    @Override
    public Result updateNote(NoteUpdateDTO noteUpdateDTO) {
        Note note = new Note();
        BeanUtils.copyProperties(noteUpdateDTO, note);
        boolean res = noteMapper.updateNote(note);
        if (res) {
            return Result.success();
        }
        return Result.error(MsgConstant.UPDATE_FAIL);
    }

    /**
     * 删除笔记
     *
     * @param noteId 笔记id
     * @return Result
     */
    @Override
    public Result deleteNote(Long noteId) {
        Note note = Note.builder()
                .id(noteId)
                .status(StatusConstant.DELETED)
                .build();
        boolean res = noteMapper.updateNote(note);
        if (res) {
            return Result.success();
        }
        return Result.error(MsgConstant.DELETE_FAIL);
    }

    /**
     * 批量删除笔记
     *
     * @param ids 笔记id
     * @return Result
     */
    @Override
    public Result deleteNotes(List<Long> ids) {
        int res = noteMapper.deleteNotes(ids);
        if (res == ids.size()) {
            return Result.success();
        } else if (res > 0 && res < ids.size()) {
            return Result.error(MsgConstant.DELETE_FAIL_PARTLY);
        } else {
            return Result.error(MsgConstant.DELETE_FAIL);
        }
    }
}
